package com.pioneer.web.book.service.impl;

import com.pioneer.web.book.domain.Book;
import com.pioneer.web.book.domain.BorrowRecord;
import com.pioneer.web.book.mapper.BookMapper;
import com.pioneer.web.book.service.IMailService;
import com.pioneer.web.system.domain.SysUser;
import com.pioneer.web.system.service.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @author aom-star@outlook.com
 * @date 2025/5/25
 * @description 发送邮件
 */
@Service
public class MailServiceImpl implements IMailService {
    @Resource
    private JavaMailSender mailSender;

    @Resource
    private ISysUserService userService;

    @Resource
    private BookMapper bookMapper;

    @Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendOverdueNotice(BorrowRecord borrowRecord) {
        SysUser user = userService.selectUserById(borrowRecord.getUserId());
        Book book = bookMapper.selectById(borrowRecord.getBookId());

        String subject = "【图书逾期提醒】";
        String text = "亲爱的 " + user.getUserName() +
                "，您借阅的《" + book.getTitle() + "》" + book.getAuthor() +
                " 应在" + borrowRecord.getReturnTime().toString() +
                "前归还，现已逾期，请尽快归还喵";

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(user.getEmail());
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }
}
